﻿using UnityEngine;


public class PsAction : MonoBehaviour //所有action的基類
{
    [Tooltip("是否自动起效")]
    public bool autoTrig = true;
    [Header("延迟多久起效")]
    public float delayTime = 1f;
    [Header("品质等级，根据分级决定是否起效")]
    public int LODLevel = 0;

    protected SmartTimer m_Timer = new SmartTimer();
    bool m_bInvalid = false;

    public virtual bool IsActived()
    {
        return isActiveAndEnabled;
    }

    public virtual void SetAllLayer(int layer)
    {

    }
    protected float m_fSpeed = 1;
    public virtual void DoSetSpeed(float fSpeed)
    {
        m_fSpeed = fSpeed;
    }

    public virtual void OnOwnerChange()
    {

    }

    public void DoPause()
    {
        if (enabled)
            m_bPause = true;
    }
    public void DoResume()
    {
        m_bPause = false;
    }
    bool m_bPause = false;


    bool m_bUseRealTimer = false;
    protected void UseRealTimer(bool bReal)
    {
        m_bUseRealTimer = bReal;
    }
    public void DoSetDelayTime(float fDelayTime)
    {
        delayTime = fDelayTime;
        m_Timer.DoReset(fDelayTime);
        enabled = true;
    }
    protected virtual void Update()
    {
        if (m_bPause)
            return;
        if (m_Timer.DoUpdate(m_fSpeed, m_bUseRealTimer))
        {
            //Debug.LogFormat("延迟激发 {0} -> {1}", delayTime, Time.time - m_fStart);// m_fDelayTime );
            TimeToTrigAction();
        }
    }

    public virtual void OnEnable()
    {
        // 这两种情况下，都是有逻辑主动调用的
        //if (GameConfig.Instance().IsGame)
        //    return;
        //if (GameConfig.Instance().IsInSkill)
        //    return;

        if (autoTrig)
        {
            StartTimer();
        }
        m_bInvalid = false;
    }

    public virtual void OnDisable()
    {

    }

    public virtual void StartTimer()
    {
        m_Timer.DoReset(delayTime);
        if (m_Timer.GetMax() <= 0)
        {
            TimeToTrigAction();
        }

    }

    protected virtual void TimeToTrigAction()
    {
        if (m_bInvalid)
            return;
        TrigAction();
    }

    public virtual bool TrigAction()
    {
        m_Timer.DoClose(); // 如果触发过了，就停止计时器

        return false;
    }


    public virtual void DoCut()
    {

    }
    public virtual void DoDelete()
    {

    }
    public virtual void DoEnd()
    {
        if (m_Timer.GetTimer() > 0)
            m_bInvalid = true;// GameObject.Destroy(this);
    }

    public virtual void DoDataCheck()
    {

    }

    public virtual bool IsOver()
    {
        if (m_Timer.GetMax() > 0)
            return !m_Timer.IsActive();
        return false;
    }
}